<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?
require_once("admin/EventViewCommon.php");
require_once("classes/ShopDB.php");
require_once("functions/datetime_func.php");
require_once('classes/Event.php'); 

class EventPropsView extends EventViewCommon {


function event_view ($event_id){
  global $_SHOP;

  $query="select * from Event LEFT JOIN Ort ON event_ort_id=ort_id 
  LEFT JOIN PlaceMap2 ON event_pm_id=pm_id
  where event_id=$event_id and event_organizer_id='{$_SHOP->organizer_id}'";
  if(!$data=ShopDB::query_one_row($query)){return;}


  $data["event_date"]=formatAdminDate($data["event_date"]);
  $data["event_time"]=formatTime($data["event_time"]);
  $data["event_open"]=formatTime($data["event_open"]);
  $event_id=$data["event_id"];

  if($data["event_status"]=='unpub'){
    $title="Publish";
    $alt="Publish";
    $src="images/publish.jpg";
    $link="publish_event.php?event_id=$event_id";
  }else if ($data["event_status"]=='pub'){
    $title="Unpublish";
    $alt="Unpublish";
    $src="images/unpublish.jpg";
    $link="unpublish_event.php?event_id=$event_id";
  }else if ($data["event_status"]=='nosal'){
    $title="Archive";
    $alt="Archive";
    $src="images/archive.jpg";
    $link="archive_event.php?event_id=$event_id";
    $title1="Publish";
    $alt1="Publish";
    $src1="images/publish.jpg";
    $link1="republish_event.php?event_id=$event_id";
 
	  $title2="Delete";
    $alt2="Delete";
    $src2="images/trash.png";
    $link2="delete_event.php?event_id=$event_id";
    
  }
  
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td colspan='2'><table border='0' width='100%' cellpadding='0' cellspacing='0'><tr><td class='admin_list_title'>".$data["event_name"]."
        </td><td align='right' width='150'>";
   if($link1){
    echo "<a href='$link1'><img border='0' alt='$alt1' title='$title1' src='$src1'></a> ";   
   }
   	
   echo "<a href='$link'><img border='0' alt='$alt' title='$title' src='$src'></a> ";
   if($link2){
    echo "<a href='$link2'><img border='0' alt='$alt2' title='$title2' src='$src2'></a> ";   
   }	

   echo "</td></tr></table></td></tr>";   
    
  $this->print_field('event_id',$data );
  $this->print_field('event_name',$data );
  $this->print_field('ort_name',$data );
  $this->print_field('event_short_text',$data );
  $this->print_field('event_text',$data );
 // $this->print_url('event_image',$data );
  $this->print_url('event_url',$data ); 
  $this->print_field('event_date',$data );
  $this->print_field('event_time',$data );
  $this->print_field('event_open',$data );
  $this->print_field('event_group_name',$data);
  
  $this->print_field_o('event_order_limit',$data );
//cc or post ---> in init
//  $this->print_field('event_payment',$data );
  $this->print_field('event_template',$data);
  $this->print_type('event_type',$data);

  echo "<tr><td class='admin_name'>".event_discounts."</td>
  <td class='admin_value'>
  <a class='link' href='view_discount.php?discount_event_id={$data['event_id']}'>".view."</a>
  </td></tr>\n";  
  
  
  $this->view_file('event_image' ,$data,$err);
  $this->view_file('event_mp3' ,$data,$err,'mp3');
    
  echo "</table>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";

}


function new_event_form (&$data, &$err){
  global $_SHOP;

  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>\n";

  $this->form_head(event_add_title);
    
  $this->print_input('event_name',$data,$err,30,100);

  $this->print_select_group('event_group_id',$data,$err);
  $this->print_select_pm('event_pm_ort_id',$data,$err); 
  $this->print_select('event_rep',$data,$err,array('unique','main'));

  $this->print_area('event_short_text',$data,$err,3);
  $this->print_area('event_text',$data,$err);
  $this->print_input('event_url',$data,$err,30,100);
  
  $this->print_date('event_date',$data,$err);
  $this->print_time('event_time',$data,$err);
  $this->print_time('event_open',$data,$err);

  $this->print_input('event_order_limit',$data,$err,3,4);
  $this->print_select_tpl('event_template',$data,$err);
 
  $this->select_types('event_type',$data,$err);
  
  
  $this->print_file('event_image',$data,$err,'img');  	
//  $this->print_file('event_ort_image',$data,$err,'img');  	
  $this->print_file('event_mp3',$data,$err,'mp3');  	


  echo "<input type='hidden' name='action' value='insert'/>\n";
  
  $this->form_foot();
  
  echo "</form>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";
}


function update_event_form (&$data, &$err){
  global $_SHOP;

  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>\n";

  $this->form_head(event_update_title);
  $this->print_field('event_id',$data);
  $this->print_input('event_name',$data,$err,30,100);
  $this->print_field_o('event_group_name',$data,$err);
  $this->print_field('ort_name',$data);
  $this->print_field('event_rep',$data);
  $this->print_area('event_short_text',$data,$err,3);
  $this->print_area('event_text',$data,$err);
  $this->print_input('event_url',$data,$err,30,100);
  $this->print_date('event_date',$data,$err);
  $this->print_time('event_time',$data,$err);
  $this->print_time('event_open',$data,$err);
  $this->print_input('event_order_limit',$data,$err,3,4);
  $this->print_select_tpl('event_template',$data,$err);
  $this->select_types('event_type',$data,$err);
  $this->print_file('event_image',$data,$err,'img');  	
//  $this->print_file('event_ort_image',$data,$err,'img');  	
  $this->print_file('event_mp3',$data,$err,'mp3');  	
  
  echo "<input type='hidden' name='event_id' value='{$data['event_id']}'/>\n";
  echo "<input type='hidden' name='action' value='update'/>\n";
  
  $this->form_foot();
  
  echo "</form>\n";
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";
}





function event_list (){
  global $_SHOP;

  $query="select * from Event LEFT JOIN Ort ON event_ort_id=ort_id WHERE event_organizer_id='{$_SHOP->organizer_id}' and event_rep!='sub' and event_status!='trash' order by event_date ";

  if(!$res=ShopDB::query($query)){
    return;
  }
  
  $alt=0;
  echo "<table class='admin_list' width='500' cellspacing='0' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='8' align='center'>".event_title."</td></tr>\n";
  
  $img_pub['pub']='images/grun.png';
  $img_pub['unpub']='images/rot.png';
  $img_pub['nosal']='images/grey.png';
  
  $alt=0;
  while($row=mysql_fetch_assoc($res)){
    $edate=formatAdminDate($row["event_date"]);
    $etime=formatTime($row["event_time"]);

     echo "<tr class='admin_list_row_$alt'> 
           <td class='admin_list_item'><img src='{$img_pub[$row['event_status']]}'></td>
           <td class='admin_list_item'>{$row['event_id']}</td>
           <td class='admin_list_item'>{$row['event_name']}</td>";
	   
     if($row['event_rep']=='main'){
     echo "<td class='admin_list_item'><a href='view_event_sub.php?event_main_id={$row['event_id']}' class='link'>".multi."</a></td>";
     }else{
     echo "<td class='admin_list_item'>$edate $etime</td>";
     }
     
     echo "<td class='admin_list_item'>{$row["ort_name"]}</td>
           <td class='admin_list_item'><a class='link' href='view_event.php?action=view&event_id={$row['event_id']}'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td> 
           <td class='admin_list_item'><a class='link' href='view_event.php?action=edit&event_id={$row['event_id']}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>";
     
     if($row['event_pm_id']){
       echo "<td class='admin_list_item'><a class='link' href='view_pm2.php?action=view_pm&pm_id={$row['event_pm_id']}'><img src='images/pm.png' border='0' alt='".place_map."' title='".place_map."'></a></td>\n";
     }else{
       echo "<td class='admin_list_item'>&nbsp;</td>";
     }

     if(($row['event_pm_id'] and $row['event_status']=='unpub') or (!$row['event_pm_id'] and $row['event_status']!='pub')){
       echo "<td class='admin_list_item'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"view_event.php?action=remove&event_id={$row['event_id']}\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>";
     }else{
       echo "<td class='admin_list_item'>&nbsp;</td>";
     }
     
     $alt=($alt+1)%2;
  }  
  
  echo "</table>\n";
  echo "<br><center><a class='link' href='view_event.php?action=add'>".add."</a></center>";
}



########################################################
########################################################
########################################################
function draw (){
  global $_SHOP;

  if($_POST['action']=='insert'){
    if(!$this->new_event_check($_POST,$err)){
      $this->new_event_form($_POST,$err,event_add_title);
    }else{
      $this->insert_event($_POST);
      $this->event_list();
    }  
  }else
  
  if($_POST['action']=='update'){
    if(!$this->update_event_check($_POST,$err)){
      $this->update_event_form($_POST,$err,event_add_title);
    }else{
      $this->update_event($_POST);
      $this->event_list();
    }  
  }else

  if($_GET['action']=='add'){
    $this->new_event_form($row,$err,event_add_title);
  }
   
  elseif($_GET['action']=='edit' and $_GET['event_id']){
    $this->event_edit($_GET['event_id']);
  }

  elseif($_GET['action']=='publish' and $_GET['event_id']){
    $event=Event::load($_GET['event_id'],FALSE);
    $event->publish();

  }elseif($_GET['action']=='view' and $_GET['event_id']){
    $this->event_view($_GET['event_id']);
    
  }elseif($_GET['action']=='remove' and $_GET['event_id']){
    $event=Event::load($_GET['event_id'],FALSE);
    $event->delete();  
    $this->event_list();

  }else{
    $this->event_list();
  }

}
########################################################
########################################################
########################################################


function new_event_check (&$data, &$err){
  global $_SHOP;
  
  if(empty($data['event_name']))    {$err['event_name']  =mandatory;}
//  if(empty($data['event_pm_id']))  {$err['event_pm_id']=mandatory;}
  
  if((isset($data['event_time-h']) and strlen($data['event_time-h'])>0) or (isset($data['event_time-m']) and strlen($data['event_time-m'])>0)){
    $h=$data['event_time-h'];
    $m=$data['event_time-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_time']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_time']=invalid;}
    else{$data['event_time']="$h:$m";}
  }

  if((isset($data['event_open-h']) and strlen($data['event_open-h'])>0) or (isset($data['event_open-m']) and strlen($data['event_open-m'])>0)){
    $h=$data['event_open-h'];
    $m=$data['event_open-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_open']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_open']=invalid;}
    else{$data['event_open']="$h:$m";}
  }

  if((isset($data['event_date-y']) and strlen($data['event_date-y'])>0) or 
     (isset($data['event_date-m']) and strlen($data['event_date-m'])>0) or 
     (isset($data['event_date-d']) and strlen($data['event_date-d'])>0)){
    $y=$data['event_date-y'];
    $m=$data['event_date-m'];
    $d=$data['event_date-d'];

    if(!checkdate($m,$d,$y)){$err['event_date']=invalid;}
    else{$data['event_date']="$y-$m-$d";}
  }

  if($data['event_rep']=='unique'){
    if(!isset($data['event_date'])){$err['event_date']=mandatory;}
    if(!isset($data['event_time'])){$err['event_time']=mandatory;}
    //if(!isset($data['event_open'])){$err['event_open']=mandatory;}
  }

  if($data['event_rep']=='unique' and $data['event_pm_ort_id']=='no_pm'){$err['event_pm_ort_id']=mandatory;}

  if($data ['event_pm_ort_id']!='no_pm'){
    list($event_pm_id,$event_ort_id)=explode(',',$data ['event_pm_ort_id']); 
    $data['event_pm_id']=$event_pm_id;
    $data['event_ort_id']=$event_ort_id;
  }  

  if($data['event_rep']=='unique'){
    $data['event_rep']='main,sub';
  }

  return empty($err);
}


function update_event_check (&$data, &$err){
  global $_SHOP;
  
  if(empty($data['event_name']))    {$err['event_name']  =mandatory;}
  
  if((isset($data['event_time-h']) and strlen($data['event_time-h'])>0) or (isset($data['event_time-m']) and strlen($data['event_time-m'])>0)){
    $h=$data['event_time-h'];
    $m=$data['event_time-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_time']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_time']=invalid;}
    else{$data['event_time']="$h:$m";}
  }

  if((isset($data['event_open-h']) and strlen($data['event_open-h'])>0) or (isset($data['event_open-m']) and strlen($data['event_open-m'])>0)){
    $h=$data['event_open-h'];
    $m=$data['event_open-m'];
    if(!is_numeric($h) or $h<0 or $h>23){$err['event_open']=invalid;}
    else if(!is_numeric($m) or $h<0 or $m>59){$err['event_open']=invalid;}
    else{$data['event_open']="$h:$m";}
  }

  if((isset($data['event_date-y']) and strlen($data['event_date-y'])>0) or 
     (isset($data['event_date-m']) and strlen($data['event_date-m'])>0) or 
     (isset($data['event_date-d']) and strlen($data['event_date-d'])>0)){
     
    $y=$data['event_date-y'];
    $m=$data['event_date-m'];
    $d=$data['event_date-d'];
    
    if(!checkdate($m,$d,$y)){$err['event_date']=invalid;}
    else{$data['event_date']="$y-$m-$d";}
  }

  return empty($err);
}


########################################################


  function insert_event (&$data){
    global $_SHOP;
    
    require_once('classes/Event.php');
    $event= new Event;
    
    $event->_fill($data);
    /*
    $event->event_name=$data['event_name'];
    $event->event_pm_id=$data['event_pm_id'];
    $event->event_ort_id=$data['event_ort_id'];
    $event->event_rep=$data['event_rep'];
    $event->event_short_text=$data['event_short_text'];
    $event->event_text=$data['event_text']; 
    $event->event_url=$data['event_url'];
    $event->event_date=$data['event_date'];
    $event->event_time=$data['event_time'];
    $event->event_open=$data['event_open'];
    $event->event_order_limit=$data['event_order_limit'];
    $event->event_template=$data['event_template'];
    $event->event_group_id=$data['event_group_id'];
    $event->event_type=$data['event_type'];
*/
    $event->event_organizer_id=$_SHOP->organizer_id;

     
    if(!$event_id=$event->save()){return;}
        
    if(!$this->photo_post($_POST,$event_id)){
      echo "<div class=error>".img_loading_problem."<div>";
    }
    
    if(!$this->photo_post_ort($_POST,$event_id)){
      echo "<div class=error>".img_loading_problem."<div>";
    }

    if(!$this->mp3_post($_POST,$event_id)){
      echo "<div class=error>".mp3_loading_problem."<div>";
    }
  }

  function event_edit ($event_id){
    global $_SHOP;
  
    $query="SELECT * FROM Event LEFT JOIN Event_group ON Event.event_group_id=Event_group.event_group_id 
    LEFT JOIN Ort ON event_ort_id=ort_id
    WHERE event_id='$event_id' and event_organizer_id='{$_SHOP->organizer_id}'";
    if(!$row=ShopDB::query_one_row($query)){
      return 0;
    }
  
    $this->update_event_form($row,$err);
  }
  
  function update_event (&$data){
    global $_SHOP;
    if($event=Event::load($data['event_id'],FALSE) and 
       $event->event_organizer_id=$_SHOP->organizer_id)
    {
      $event->_fill($data);
      if(!$event->save()){return;}
       

      if(!$this->photo_post($_POST,$_POST['event_id'])){
        echo "<div class=error>".img_loading_problem."<div>";
      }
    
      if(!$this->photo_post_ort($_POST,$_POST['event_id'])){
        echo "<div class=error>".img_loading_problem."<div>";
      }
      if(!$this->mp3_post($_POST,$_POST['event_id'])){
        echo "<div class=error>".mp3_loading_problem."<div>";
      }
    }
  }  
}
?>
